#include "stdinc.h"
#include "Console.h"

#include "cmds.h"
#include "util/FSystem.h"

CConsole::CConsole()
{
	m_list.AddCmdList(GetCmds_Main());
}

CConsole::~CConsole()
{
}

bool CConsole::Exec(char* text)
{
	return m_list.Exec(text);
}

bool CConsole::Write(char* text)
{
	return false;
}

bool CConsole::ParseConfig(char* text)
{
	int p = 0;
	while (text[p])
	{
		while ((text[p]=='\n')||(text[p]=='\r'))
			p++;

		int p1 = p;

		while (text[p1] && (text[p1] != '\n') && (text[p1] != '\r'))
		{
			p1++;
		}

		char c = text[p1];
		text[p1] = 0;

		if (!Exec(&text[p]))
		{
			text[p1] = c;
			return false;
		}

		text[p1] = c;
		p = p1;
	}

	return true;
}

bool CConsole::ParseConfigFile(char* fname)
{
	IFile *f = FSYSTEM->Open(fname, CFileSys::READ);

	if (!f)
		return false;

	char* data = new char[f->GetSize()];
	f->Read(data, f->GetSize());
	FSYSTEM->Close(f);

	bool res = ParseConfig(data);
	SAFE_DEL(data);
	
	return res;
}